<?php
header("Content-type:text/html; charset=utf-8");
require_once ('config.php');
require_once (WEBURL.'/include/conn.php');
require_once ('login_status_check.php');
if(!checkLogin()){
	require_once ('admin_login.php');
}
class Article{
	private $article_information;
	private $chanel_name;
	private $channel_visit_path;
	private $page_status;
	function __construct(){
		$this->article_information="";
		$this->chanel_name="";
		$this->channel_visit_path="";
	}
	function getArticleInformation($channel_id=0,$limit=20,$offset=0){
		if($limit<0){$limit=20;}
		if($offset<0){$offset=0;}
		$extend_get_num="";
		$extend_get_info="";
		$page_link="article_show.php";
		$this->article_information="";
		
		/*获取文章个数*/
		if($channel_id!=0){
			$sql_get_articles_number="select * from article_normal where article_main_channel=$channel_id"." ".$extend_get_num;
		}else{
			$sql_get_articles_number="select * from article_normal ".$extend_get_info;
		}
		$result_get_articles_number=mysql_query($sql_get_articles_number) or die(mysql_error());
		$numrows=mysql_num_rows($result_get_articles_number);
		if($offset>$numrows){$offset=($numrows%$limit==0)?($numrows-$limit):($numrows-($numrows%$limit));}
		if($offset%$limit!=0){$offset=$offset-($offset%$limit);}
		/*获取文章信息*/
		if($channel_id!=0){
			$sql_get_articles_information="select * from article_normal where article_main_channel=$channel_id ".$extend_get_num." order by article_modify_time desc limit $offset,$limit";
			$this->getArticleChannelInformation($channel_id);
		}else{
			$sql_get_articles_information="select * from article_normal ".$extend_get_info."order by article_modify_time desc limit $offset,$limit";
		}
		
		$result_get_articles_information=mysql_query($sql_get_articles_information) or die(mysql_error());
		/*显示查询结果*/
		while ($data=mysql_fetch_row($result_get_articles_information)) {
			if($channel_id==0){
				$this->getArticleChannelInformation($data[6]);
			}
			if($data[2]==1){
				$link=$data[3];
			}else {
				$link="../article.php?article_id=$data[0]&channel_id=$data[6]";
			}
			$modify_time=date('Y-m-d H:i:s',$data[12]);
            $extends_link=($data[2]==1)?"<span class='extends_link'>[链]</span>":"";
            $picture=($data[8]==1)?"<span class='picture'>[图]</span>":"";
            $is_top=($data[14]==1)?"<span class='is_top'>[顶]</span>":"";
            $article_title="<a href='$link' target='_blank' title='点击查看'>".$data[1].$extends_link.$picture.$is_top."</a>";
            $article_operator="<a href='$link' target='_blank'>[查看]</a>";
            $this->article_information.="
              <tr class='article' onmousemove=\"javascript:this.style.background='#ddd';\" onmouseout=\"javascript:this.style.background='';\">
              <td><input type='checkbox' name='article[]' value='$data[0]'/></td><td><span>$data[0]</span></td>
              <td class='article_title'>$article_title</td>
              <td><span>$this->channel_name</span></td>
              <td><span>$data[5]</span></td>
              <td><span>$modify_time</span></td>
              <td><span>$data[10]</span></td>
              <td><span>$article_operator<a href='article_modify.php?article_id=$data[0]'>[修改]</a><a href='article_remove.php?article_id=$data[0]&article_title=$data[1]'>[删除]</a></span></td>
              </tr>";
		}
		$this->article_information.="<tr align='center'><td colspan='10' class='page_navigation'>";
		
		/*文章数*/
		$this->article_information.="<span class='article_num'>共有文章".$numrows."篇</span>";
		/*首页*/
		$this->article_information.="<span class='page'><a href='$page_link?offset=0&channel_id=$channel_id'>首页</a></span>";
		/*上一页*/
		if ($offset>0) {
			$prevoffset=$offset-$limit;
			if($prevoffset>=0)
			$this->article_information.="<span class='page'><a href='$page_link?offset=$prevoffset&channel_id=$channel_id'>上一页</a></span>";
		}
		
		/*计算页面数*/
		$pages=intval($numrows/$limit);
		
		/*如果有余数，加一*/
		if ($numrows%$limit) {
			$pages++;
		}
		/*显示页数*/
		   $current_num=$offset/$limit+1;
		   $i=($current_num>3)?($current_num-2):1;
		   $limit_num=$i+5;
		   while($i<=$pages&&$i<$limit_num) {
		   	$newoffset=$limit*($i-1);
		    $page_num=$i;
		    if($i==$current_num){$page_num="<span class='current_page'>$i</span>";}
			$this->article_information.="<span class='page'><a href=\"$page_link?offset=$newoffset&channel_id=$channel_id\">$page_num</a></span>";
		    $i++;
		   }
		
		/*下一页*/
		if (!(($offset/$limit)==$pages) && $pages!=1) {
		$newoffset=$offset+$limit;
		if($newoffset<$numrows)
		$this->article_information.="<span class='page'><a href=\"$page_link?offset=$newoffset&channel_id=$channel_id\">下一页</a></span>";
		}
		/*尾页*/
		$newoffset=($numrows%$limit)?($numrows-$numrows%$limit):($numrows-$numrows%$limit-$limit);
		$this->article_information.="<span class='page'><a href='$page_link?offset=$newoffset&channel_id=$channel_id'>尾页</a></span>";
		$this->article_information.="</td></tr>";
		return $this->article_information;
	}

	
	function getArticleChannelInformation($channel_id){
		$sql_get_article_channel_information="select channel_name,channel_visit_path from channel where channel_id=$channel_id";
		$result_get_article_channel_information=mysql_query($sql_get_article_channel_information) or die(mysql_error());
		$row_get_article_channel_information=mysql_fetch_row($result_get_article_channel_information);
		$this->channel_name=$row_get_article_channel_information[0];
		$this->channel_visit_path=$row_get_article_channel_information[1];
	}
}